package com.lxf.log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日志类
 */
public class MyLog {
	private String name = "lxf";	
	
	MyLog(){}
	
	MyLog(String name){
		this.name = name;
	}

	/**
	 * 记录日志到文件
	 * @param str	�ַ�
	 */
	public void w(String str){
		File f =new File("c:/"+name+".log");
		if(!f.exists()){
			try {
				f.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			FileWriter fw = new FileWriter(f,true);
			PrintWriter pw=new PrintWriter(fw);
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			pw.println(df.format(new Date()));
			pw.println(str);
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}  
	}
	
	/**
	 * 记录日志
	 * @param e	exception
	 */
	public  void w(Exception e){
		StringWriter sw = new StringWriter(); 
        e.printStackTrace(new PrintWriter(sw, true)); 
        w(sw.toString());
	}
	
}
